// pages/shop_coupon/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{ addCard ,cardList,card_stop} from '../../api/index'
import {formatTime} from '../../utils/util'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],

    add_coupon:false,
    checked:false,

    time_show:false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),

    form:{
      name:'',
      man:'',
      jian:'',
      time:''
    },
    page:1,
    pagesize:10,
    page_show:true,
    page_text:'加载中...'
  },
  getCardList(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      shop_id:wx.getStorageSync('shop_id'),
      page:this.data.page,
      pagesize:this.data.pagesize,
    }
    cardList(obj).then(res=>{
      if(res.msg.length <= 0){
        return this.setData({
          page_show:false,
          page_text:'加载完毕！'
        })
      }
      if(res.sta == 1){
        if(res.msg.length < this.data.pagesize){
          this.setData({
            page_show:false,
            page_text:'加载完毕！'
          })
        }
        this.setData({
          list:[...this.data.list,...res.msg]
        })
      }
    })
  },
  submit(){
    var obj = {
      shop_id:wx.getStorageSync('shop_id'),
      uid:wx.getStorageSync('uid'),
      name:this.data.form.name,
      man:this.data.form.man,
      jian:this.data.form.jian,
      endtime:this.data.currentDate / 1000
    }
    addCard(obj).then(res=>{
      if(res.sta == 1){
        showText(res.msg,1)
        this.setData({
          page:1,
          pagesize:this.data.pagesize,
          page_show:true,
          list:[]
        })
        this.getCardList()
        this.setData({
          form:{
            name:'',
            man:'',
            jian:'',
            time:''
          }
        })
      }
      this.setData({
        add_coupon:false
      })
    })
  },
  cancel(e){
    this.setData({
      form:{
        name:'',
        man:'',
        jian:'',
        time:''
      },
      add_coupon:false
    })
  },
  time_confirm(e){
    this.setData({
      time_show:false,
      'form.time':formatTime(new Date(e.detail))
    })
  },

  time_cancel(e){
    this.setData({
      time_show:false
    })
  },
  click(e){
    onClick(e,this)
  },
  change(e){
    onInput(e,this)
  },
  removeFn(e){
    Dialog.confirm({
      title: '确定要删除吗？'
    })
    .then((res) => {
      const id = e.currentTarget.dataset.id
      var obj = {
        uid:wx.getStorageSync('uid'),
        card_id:id,
      }
      card_stop(obj).then(res=>{
        if(res.sta == 1){
          showText('删除成功！',1)
          this.setData({
            page:1,
            pagesize:this.data.pagesize,
            page_show:true,
            list:[]
          })
          this.getCardList()
        }else{
          showText('删除失败！',2)
        }
      })
    })
    .catch((res) => {
      console.log(res)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      page:1,
      pagesize:this.data.pagesize,
    })
    this.getCardList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      pagesize:this.data.pagesize,
      page_show:true,
      list:[]
    })
    this.getCardList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page+1,
    })
    if(this.data.page_show){
      this.getCardList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})